<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\member\model;

class Level extends \app\BaseModel
{
    protected $name = "massage_member_level";
    protected $append = ["rights", "coupon"];
    public function getRightsAttr($value, $data)
    {
        if(isset($data["id"])) {
            $connect_model = new RightsConnect();
            $dis = ["a.level_id" => $data["id"], "b.status" => 1];
            $list = $connect_model->alias("a")->join("massage_member_rights b", "a.rights_id = b.id")->where($dis)->field("a.*,b.title,b.key,b.show_title,b.rights_icon")->group("a.rights_id")->order("b.top desc,a.id desc")->select()->toArray();
            return $list;
        }
    }
    public function getCouponAttr($value, $data)
    {
        if(isset($data["id"])) {
            $coupon = new Coupon();
            $dis = ["a.level_id" => $data["id"], "b.status" => 1];
            $list = $coupon->alias("a")->join("massage_service_coupon b", "a.coupon_id = b.id")->where($dis)->field("a.*,b.title,b.full,b.discount,(b.discount*a.num) as total_discount")->group("a.coupon_id")->order("a.id desc")->select()->toArray();
            return $list;
        }
    }
    public function dataAdd($data)
    {
        $data["create_time"] = time();
        $res = $this->insert($data);
        return $res;
    }
    public function dataUpdate($dis, $data)
    {
        $res = $this->where($dis)->update($data);
        return $res;
    }
    public function dataList($dis, $page)
    {
        $data = $this->where($dis)->order("growth,id desc")->paginate($page)->toArray();
        return $data;
    }
    public function dataInfo($dis)
    {
        $data = $this->where($dis)->find();
        return !empty($data) ? $data->toArray() : [];
    }
    public function getUserLevel($user_id, $is_auth = 1)
    {
        $start = strtotime(date("Y-01-01"));
        $user_model = new \app\massage\model\User();
        $growth_model = new Growth();
        $user = $user_model->dataInfo(["id" => $user_id]);
        if(empty($user)) {
            return [];
        }
        if($is_auth == 1) {
            $m_auth = new \app\member\info\PermissionMember($user["uniacid"]);
            if(!$m_auth->pAuth()) {
                return [];
            }
            $config_model = new Config();
            $config = $config_model->dataInfo(["uniacid" => $user["uniacid"]]);
            if($config["status"] == 0) {
                return [];
            }
        }
        $dis[] = ["status", "=", 1];
        $dis[] = ["uniacid", "=", $user["uniacid"]];
        $dis[] = ["growth", "<=", $user["growth"]];
        $data = $this->where($dis)->order("growth desc,id desc")->find();
        if($user["member_calculate_time"] < $start) {
            if(!empty($data)) {
                $find = $this->where(["status" => 1, "uniacid" => $user["uniacid"]])->where("growth", "<", $data["growth"])->order("growth desc,id desc")->find();
                if(!empty($find)) {
                    $del_growth = $user["growth"] - $find["growth"];
                } else {
                    $del_growth = $user["growth"];
                }
                $admin_model = new \app\massage\model\Admin();
                $create_user = $admin_model->where(["is_admin" => 1])->value("id");
                $growth_model->addRecord($del_growth, 0, $user, 3, 0, $create_user);
            }
            $user_model->dataUpdate(["id" => $user_id], ["member_calculate_time" => $start]);
            $data = $this->where($dis)->order("growth desc,id desc")->find();
        }
        if(!empty($data)) {
            $data["vip"] = $this->where("status", ">", -1)->where("growth", "<=", $data["growth"])->count();
        }
        return $data;
    }
    public function getUserLevelV2($user, $create_user)
    {
        $growth_model = new Growth();
        $user_model = new \app\massage\model\User();
        $start = strtotime(date("Y-01-01"));
        $dis[] = ["status", "=", 1];
        $dis[] = ["uniacid", "=", $user["uniacid"]];
        $dis[] = ["growth", "<=", $user["growth"]];
        $data = \think\facade\Db::name("massage_member_level")->where($dis)->order("growth desc,id desc")->find();
        if($user["member_calculate_time"] < $start) {
            if(!empty($data)) {
                $find = $this->where(["status" => 1, "uniacid" => $user["uniacid"]])->where("growth", "<", $data["growth"])->order("growth desc,id desc")->find();
                if(!empty($find)) {
                    $del_growth = $user["growth"] - $find["growth"];
                } else {
                    $del_growth = $user["growth"];
                }
                $growth_model->addRecord($del_growth, 0, $user, 3, 0, $create_user);
            }
            $user_model->dataUpdate(["id" => $user["id"]], ["member_calculate_time" => $start]);
            $data = \think\facade\Db::name("massage_member_level")->where($dis)->order("growth desc,id desc")->find();
        }
        return !empty($data) ? $data["title"] : "";
    }
    public function checkAuthData($data)
    {
        $member_info = $this->getUserLevel($data["id"]);
        if(!empty($member_info)) {
            $arr["member_info"]["title"] = $member_info["title"];
        } else {
            $arr["member_info"] = [];
        }
        return $arr;
    }
    public function updateUserGrowth($user_id, $uniacid, $is_add, $growth, $type, $order_id = 0, $create_user = 0)
    {
        $growth = round($growth);
        $config_model = new Config();
        $growth_model = new Growth();
        $user_model = new \app\massage\model\User();
        $record_model = new UpRecord();
        $user = $user_model->dataInfo(["id" => $user_id]);
        if(empty($user)) {
            return false;
        }
        $config = $config_model->dataInfo(["uniacid" => $uniacid]);
        if($config["status"] == 0) {
            return false;
        }
        \think\facade\Db::startTrans();
        $level = $this->getUserLevel($user_id);
        if($config["growth_limit"] == 1 && $is_add == 1 && $type != 2) {
            $today_growth = $growth_model->where(["user_id" => $user_id, "is_add" => 1, "status" => 1])->whereTime("create_time", "today")->sum("growth");
            $c_growth = $config["max_growth_day"] - $today_growth;
            $growth = $growth < $c_growth ? $growth : $c_growth;
        }
        if(0 < $growth) {
            $id = $growth_model->addRecord($growth, $is_add, $user, $type, $order_id, $create_user);
        }
        $next_level = $this->getUserLevel($user_id);
        $start_level_id = !empty($level) ? $level["growth"] : 0;
        $next_level_id = !empty($next_level) ? $next_level["growth"] : 0;
        if($start_level_id < $next_level_id) {
            $up_record = $record_model->dataInfo(["user_id" => $user_id, "level_id" => $next_level["id"]]);
            $coupon_model = new \app\massage\model\CouponRecord();
            $rights = $next_level["rights"];
            if(!empty($rights) && empty($up_record)) {
                foreach ($rights as $v) {
                    if(in_array($v["key"], ["send_coupon"])) {
                        $coupon = $next_level["coupon"];
                        if(!empty($coupon)) {
                            foreach ($coupon as $value) {
                                $coupon_model->recordAdd($value["coupon_id"], $user_id, $value["num"]);
                            }
                        }
                    }
                }
            }
            $growth_model->dataUpdate(["id" => $id], ["is_up" => $next_level["id"]]);
            $insert = ["uniacid" => $uniacid, "user_id" => $user_id, "level_id" => $next_level["id"], "level" => $next_level["vip"]];
            $record_model->dataAdd($insert);
        }
        \think\facade\Db::commit();
        return true;
    }
    public function initMemberRights($user_id)
    {
        $level = $this->getUserLevel($user_id);
        if(!empty($level)) {
            $record_model = new UpRecord();
            $up_record = $record_model->dataInfo(["user_id" => $user_id, "level_id" => $level["id"]]);
            $coupon_model = new \app\massage\model\CouponRecord();
            $rights = $level["rights"];
            if(!empty($rights) && empty($up_record)) {
                foreach ($rights as $v) {
                    if(in_array($v["key"], ["send_coupon"])) {
                        $coupon = $level["coupon"];
                        if(!empty($coupon)) {
                            foreach ($coupon as $value) {
                                $coupon_model->recordAdd($value["coupon_id"], $user_id, $value["num"]);
                            }
                        }
                    }
                }
                $insert = ["uniacid" => $level["uniacid"], "user_id" => $user_id, "level_id" => $level["id"], "level" => $level["vip"]];
                $record_model->dataAdd($insert);
            }
        }
        return true;
    }
    public function levelUp($order, $type = 1, $is_car = 1)
    {
        $m_auth = new \app\member\info\PermissionMember($order["uniacid"]);
        if(!$m_auth->pAuth()) {
            return false;
        }
        $config_model = new Config();
        $config = $config_model->dataInfo(["uniacid" => $order["uniacid"]]);
        if($config["status"] == 0) {
            return false;
        }
        if($order["pay_model"] == 2) {
            return false;
        }
        if($type == 1) {
            $refund_model = new \app\massage\model\RefundOrder();
            $refund_price = $refund_model->where(["order_id" => $order["id"], "status" => 2])->sum("refund_price");
            $price = round($order["pay_price"] - $refund_price, 2);
        } elseif($type == 4) {
            $price = $order["pay_price"];
        }
        $growth = $config["growth_value"] * $price;
        $res = $this->updateUserGrowth($order["user_id"], $order["uniacid"], 1, $growth, $type, $order["id"]);
        return $res;
    }
}

?>